#!/bin/bash
# Tar-gz-Package-Builder


#variable declariations
RESOLUTIONS="8 16 22 24 32 48 256";
FILERESOLUTIONS="16 22 24 32 256";




#start the script
echo "Building a new release may take up to 25 minutes.";
echo;

echo -n "Preparing.(1 of 9).................................";
cd ../ #moving out of working copy
mkdir $THEME; #create UltimateGnome dir
cp -f -p $ICONFILESDIR/256x256/index.theme $THEME/index.theme; #index.theme for PNG version
cp -f -p $PROJECT/GPL $THEME/GPL; #copying license
cd $THEME; #move to exporting dir
echo "[DONE]";

echo -n "Creating directories and subdirectories.(2 of 9)...";
for res in $RESOLUTIONS
do
	mkdir $res"x"$res;
	cd $res"x"$res
	for folder in $FOLDERS
	do
		mkdir $folder;
	done
	cd ..
done
echo "[DONE]";


echo -n "Exporting files.(3 of 9)...........................";
cd ../$ICONFILESDIR;
for res in $FILERESOLUTIONS #this is to prevent scalable icon files to be used
do
	FILES=`find ./$res"x"$res -name *.icon`                        
	for file in $FILES
	do
		cp -f -p $file ../../$THEME/$file;
	done
done
cd ../$SOURCES
echo "[DONE]";

echo -n "Exporting symlinks.(4 of 9)........................";
LINKS=`find ./ -type l`
for file in $LINKS
do
	for res in $RESOLUTIONS
	do
		cp -f -p -R $file ../../$THEME/$res"x"$res/$file;
	done
done
cd ../../$THEME; 
echo "[DONE]";

echo -n "Adjusting symlinks.(5 of 9)........................";
for res in $RESOLUTIONS
do
	cd $res"x"$res
	for folder in $FOLDERS
	do
		cd $folder
		sh ../../../$SUBSCRIPTDIR/Symlink-Fixer.sh;
		cd ..
	done
	cd ..
done #here we are on $HOME/UltimateGnome
echo "[DONE]";

echo -n "Exporting icons.(6 of 9)...........................";
cd ../$PROJECT/$SOURCES
SVG=`find ./ -name *.svg -type f`                           
for file in $SVG
do
	for res in $RESOLUTIONS
	do
		inkscape --without-gui --export-area-canvas --export-width=$res --export-height=$res --file=$file --export-png=../../$THEME/$res"x"$res/$file >InkscapeExportOutput.tmp
	done
done 
rm -f InkscapeExportOutput.tmp
echo "[DONE]";

echo -n "Renaming icons.(7 of 9)............................";
#use the same SVG list from the previous block                          
for file in $SVG
do
	BEGIN="/"; #file ./name.svg
	POS=`expr index "$file" $BEGIN`; #position of /name.svg
	POS=`expr $POS + 1`; #position of name.svg
	LENGTH=${#file}; #this is the length of ./name.svg
	file=`expr substr "$file" $POS $LENGTH`; #file now has name.svg
	oldfile=$file; #oldfile has name.svg
	LENGTH=${#file}; #this is the length of name.svg
	LENGTH=`expr $LENGTH - 4`; #this is the length of name
	file=`expr substr "$file" 1 $LENGTH`;
	file=$file".png"; #file has the name that the new symlink should have name.png
	for res in $RESOLUTIONS
	do
		mv ../../$THEME/$res"x"$res/$oldfile ../../$THEME/$res"x"$res/$file;
	done
done
echo "[DONE]";

echo -n "Removing useless files.(8 of 9)....................";  
cd ../../$THEME #move up to UltimateGnome
sh ../$SUBSCRIPTDIR/Useless-Files-Remover.sh;
echo "[DONE]";

echo -n "Creating 'tar.gz' package.(9 of 9).................";
cd ../ #moving out of the exporting directory
tar --create --gzip --file=$THEME.0.0.0.tar.gz $THEME            #create the .tar.gz package 

rm -R -f $THEME                                                         #remove the copied directory
echo  "[DONE]"; 
echo "Release creation process completed.";

echo;

exit 0
